//命名空间
var publicJsUtil={};

/**
 * 日期格式化
 * @author yangLi
 * @param value
 * @param row
 * @param index
 * @returns {String}
 */
publicJsUtil.formatterDate = function (value,row,index)
{
	if(value!=null){
		var unixTimestamp = new Date(value); 
		return unixTimestamp.getFullYear()+'-'+(unixTimestamp.getMonth()+1)+'-'+unixTimestamp.getDate();
	    //return unixTimestamp.toLocaleString();
	}
	 

};

/**
 * 时间格式化
 * @author wangdong
 * @param value
 * @param row
 * @param index
 * @returns {String}
 */
publicJsUtil.formatterTime = function (value,row,index)
{
	if(value!=null){
		var unixTimestamp = new Date(value); 
		var hour = unixTimestamp.getHours() < 10 ? "0" + unixTimestamp.getHours() : unixTimestamp.getHours(); 
		var minute = unixTimestamp.getMinutes() < 10 ? "0" + unixTimestamp.getMinutes() : unixTimestamp.getMinutes();
		var second = unixTimestamp.getSeconds() < 10 ? "0" + unixTimestamp.getSeconds() : unixTimestamp.getSeconds(); 
		return unixTimestamp.getFullYear()+'-'+(unixTimestamp.getMonth()+1)+'-'+unixTimestamp.getDate()+' '+hour+':'+minute+':'+second;
	    //return unixTimestamp.toLocaleString();
	}
	 

};



/**
 * 性别格式化
 * @author yangLi
 * @param value
 * @param row
 * @param index
 * @returns {String}
 */
publicJsUtil.formatteSex = function (value,row,index)
{
	value = $.trim(value);
	
	if("M"==value)
	{
		return "男";
	}
	else if("W"==value)
	{
		return "女";
	}
	else
	{
		return value;
	}
};

publicJsUtil.formatteGrade = function (value,row,index)
{
	if("1"==value)
	{
		return "初级";
	}
	else if("2"==value)
	{
		return "中级";
	}
	else if("3"==value)
	{
		return "高级";
	}
	else
	{
		return "";
	}
};

/**
 * 对日期字段编辑的时候，格式化。
 * @author yangLi
 * @param sd
 * @returns Date
 */
publicJsUtil.timestampToDate = function(sd){
	if (!sd)
	{
		return new Date();
	} 
	var unixTimestamp = new Date(sd); 
	return new Date(unixTimestamp.getFullYear()+'-'+(unixTimestamp.getMonth()+1)+'-'+unixTimestamp.getDate());
};

//格式化日期控件
publicJsUtil.fd = function(sd){
	if (!sd) return new Date();
	var s = timeStamp2String(sd);
		var ss = s.split('-');
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			data1 = new Date(y,m-1,d);
			return new Date(y,m-1,d);
		} else {
			data1=new Date();
			return new Date();
		}
};
function timeStamp2String(time){
    var datetime = new Date();
    if(time!=null){
    	if(typeof time=="string"){
    		var y = parseInt(time.substr(0,4));
	       	 var m = parseInt(time.substr(5,2));
	       	 var d = parseInt(time.substr(8,2));
	       	 datetime.setYear(y);
	       	 datetime.setMonth(m-1);
	       	 datetime.setDate(d);
    	}else{
    		datetime.setTime(time);
    	}
    	 
    }
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    return year + "-" + month + "-" + date;
}



/**
 * 获取URL后面参数的值
 * @author yangLi
 */
publicJsUtil.getUrlParam= function (name){  
    //构造一个含有目标参数的正则表达式对象  
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");  
    //匹配目标参数  
    var r = window.location.search.substr(1).match(reg);  
    //返回参数值  
    if (r!=null) return unescape(r[2]);  
    return null;  
};


/**
 * 将字符串格式化成html内容
 */
function formateOpt(value,row,index){
	return value;
}

/**
 * 格式化日期控件
 */
publicJsUtil.timestampToString = function(sd){
	if (!sd) return new Date();
	var s = timeStamp2String(sd);
		var ss = s.split('-');
		var y = parseInt(ss[0],10);
		var m = parseInt(ss[1],10);
		var d = parseInt(ss[2],10);
		if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
			data1 = new Date(y,m-1,d);
			return new Date(y,m-1,d);
		} else {
			data1=new Date();
			return new Date();
		}

};
//timestamp转String在datagrid中formatter直接调用
publicJsUtil.timeStamp2String = function(value,row,index){
	if(value==null) return "";
    var dates = timeStamp2String(value);
    return dates;
};
//timestamp转String
function timeStamp2String(time){
    var datetime = new Date();
    if(time!=null){
    	if(typeof time=="string"){
    		var y = parseInt(time.substr(0,4));
	       	 var m = parseInt(time.substr(5,2));
	       	 var d = parseInt(time.substr(8,2));
	       	 /*datetime.setYear(y);
	       	 datetime.setMonth(m-1);
	       	 datetime.setDate(d);*/
	      	datetime=new Date(y,m-1,d);
    	}else{
    		datetime.setTime(time);
    	}
    }
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    return year + "-" + month + "-" + date;
}
/**
 * 清空按钮事件，
 * 按钮ID必须为reset
 * 清空表单ID必须为 q_form 的文本框
 * 事件。
 */
$("#reset").bind("click",function(){
    $(':input','#q_form')  
    .not(':button, :submit, :reset, :hidden')  
    .val('')  
    .removeAttr('checked')  
    .removeAttr('selected');
});


//读取身份证:用于审查页面
publicJsUtil.readidcard_check = function(tablename,r){
	if(r.status=="1"){
		app.msg.info("读取身份证信息失败，请检查设备是否接入正常并重试！");
		return ;
	}else if(r.status=="0"){
		var entity={};
		entity.identityno=r.peopleIDCode;
		entity.empsex=r.peopleSex;
		entity.empname=r.peopleName;
		entity.nation=r.peopleNation;
		entity.birthday=r.peopleBirthday;
		entity.address=r.peopleAddress;
		entity.validdate=r.endDate;
		entity.photo=r.picture;
		entity.tablename=tablename;
		 app.ajax({url: app.approot+"empcheck/updateempscan",data:entity,success:function(results){
				if (results.success){
					 app.msg.info(results.msg);
		        }else{
		        	 app.msg.info(results.msg);
		        	return;
		        }
			},error:function(){
				alert("error");
			}});
	}
	
	/*
	//var strReadResult="0";
	var entity={};
	
	  // var CVR_IDCard = document.getElementById("CVR_IDCard");					
	 	var strReadResult = CVR_IDCard.ReadCard();
		if(strReadResult == "0"){
			var identityno="140421197901214839";
			var sex="男";
			var name="MM";
			var birth="19890924";
			var birthday=birth.substring(0,4)+"-"+birth.substring(4,6)+"-"+birth.substring(6,8);
			var validdate="2012-01-01";
			var identityno=CVR_IDCard.CardNo;
			var sex=CVR_IDCard.Sex;
			var name=CVR_IDCard.Name;
			var validdate=CVR_IDCard.ExpiredDate;
			var nation=CVR_IDCard.Nation;
			var address=CVR_IDCard.Address;
			var birth=CVR_IDCard.Born;
			var birthday=birth.substring(0,4)+"-"+birth.substring(4,6)+"-"+birth.substring(6,8);
			var timestamp=new Date().getTime();
			
			entity.identityno=identityno;
			entity.empsex=sex;
			entity.empname=name;
			entity.nation=nation;
			entity.birthday=birthday;
			entity.address=address;
			entity.validdate=validdate.substring(0,4)+"-"+validdate.substring(4,6)+"-"+validdate.substring(6,8);;
			entity.operdate=timestamp;
			entity.tablename=tablename;
			 app.ajax({url: app.approot+"empcheck/updateempscan",data:entity,success:function(results){
					if (results.success){
						 app.msg.info(results.msg);
			        }else{
			        	 app.msg.info(results.msg);
			        	return;
			        }
				},error:function(){
					alert("error");
				}});
		}else{
			 app.msg.info(strReadResult);
		}
*/};




//读取身份证 :用于备案申报
publicJsUtil.readidcard = function(tablename,r){
	/*var entity={};
	app.ajax({url: app.approot+"titleempreg/updateempscan",data:entity,success:function(results){
		if (results.success){
			 app.msg.info(results.msg);
        }else{
        	 app.msg.info(results.msg);
        	return;
        }
	},error:function(){
		alert("error");
	}});*/
	
	if(r.status=="1"){
		app.msg.info("读取身份证信息失败，请检查设备是否接入正常并重试！");
		return ;
	}else if(r.status=="0"){
		var entity={};
		entity.identityno=r.peopleIDCode;
		entity.empsex=r.peopleSex;
		entity.empname=r.peopleName;
		entity.nation=r.peopleNation;
		entity.birthday=r.peopleBirthday;
		entity.address=r.peopleAddress;
		entity.validdate=r.endDate;
		entity.photo = r.picture;
		entity.tablename=tablename;
		 app.ajax({url: app.approot+"titleempreg/updateempscan",data:entity,success:function(results){
				if (results.success){
					 app.msg.info(results.msg);
		        }else{
		        	 app.msg.info(results.msg);
		        	return;
		        }
			},error:function(){
				alert("error");
			}});
	}
	
	};

publicJsUtil.openAffixdialog = function(title,url){
	
	var option = {
    		title:title,
    		src:url,
    		width:screen.availWidth,
    		height:screen.availHeight,
    		resizable:true,
    		onBeforeClose:function(){
    		},
    		onBeforeDestroy:function(){
    		},
    		onload:function(){
    			//if(row)
    			//option.iwin.$("#fm").form("load",row);
    		}
    		
    	};
	var editor = app.dialog(option);
	
}



//填写说明弹出框
function showInstructions(url){
	  var url = app.approot+url;
	  option = {
	  		title:"填报说明",//标题
	  		src:url, //路径
	  		width:600,
	  		height:400,
	  		onload:function(){ //加载
	
	  		},
	  		buttons:
	  			[
					{
						 text:' 关闭 ',
						 iconCls:'',
						 handler:function(){	            	
							 editor.dialog("destroy");//关闭
							 editor.remove();
						}  
					}]
	  	};
	  editor = app.dialog(option);
}
//计算2个日期的时间差
function  DateDiff(sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
    var  aDate,  oDate1,  oDate2,  iDays  
    aDate  =  sDate1.split("-")  
    oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
    aDate  =  sDate2.split("-")  
    oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
    iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
    return  iDays  
} 


//验证 18位社会信用代码
publicJsUtil.valid18entpcode =function(entpcode18){
	var entpcode17=entpcode18.substring(0,17);
	//1.得出各个位置上字符值 Ci;
	var CI=new Array();
	var object={"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,
			"A":10,"B":11,"C":12,"D":13,"E":14,"F":15,"G":16,
			"H":17,"J":18,"K":19,"L":20,"M":21,"N":22,
			"P":23,"Q":24,"R":25,"T":26,"U":27,"W":28,"X":29,"Y":30};

	for(var i=0;i<entpcode17.length;i++){
		var entpchar=entpcode17.substring(i,i+1);
		CI.push(object[entpchar]);
	}
	
	
	//2.定义前17位的加权因子
	var WI=[1,3,9,27,19,26,16,17,20,29,25,13,8,24,10,30,28];
	
	
	var sum=0;
	//3,4. 计算级数之和
	for(var i=0;i<CI.length;i++){
		sum+=WI[i]*CI[i];
	}
	//5.计算整数求余函数
	var MOD=0;
	MOD=sum%31;
	//6.得出校验码
	var C18=0;
	C18=31-MOD;
	if(C18==31){
  		C18="0";	
  	}else{
  		for(var i in object){
  			if(C18==object[i]){
  				C18=i;
  				break;
  			}
  		}
  		//C18=object[C18];
  	}
	if(entpcode18.substring(17,18)!=C18){
		return false;
	}else{
		return true;
	}
}

//验证 9位组织机构代码
publicJsUtil.valid9entpcode =function (entpcode9){
	var entpcode8=entpcode9.substring(0,8);
	//1.得出各个位置上字符值 Ci;
	var CI=new Array();
	var object={"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,
			"A":10,"B":11,"C":12,"D":13,"E":14,"F":15,"G":16,
			"H":17,"I":18,"J":19,"K":20,"L":21,"M":22,"N":23,
			"O":24,"P":25,"Q":26,"R":27,"S":28,"T":29,
			"U":30,"V":31,"W":32,"X":33,"Y":34,"Z":35};

	for(var i=0;i<entpcode8.length;i++){
		var entpchar=entpcode8.substring(i,i+1);
		CI.push(object[entpchar]);
	}
	
	
	//2.定义前17位的加权因子
	var WI=[3,7,9,10,5,8,4,2];
	
	
	var sum=0;
	//3,4. 计算级数之和
	for(var i=0;i<CI.length;i++){
		sum+=WI[i]*CI[i];
	}
	//5.计算整数求余函数
	var MOD=0;
	MOD=sum%11;
	//6.得出校验码
	var C9=0;
	C9=11-MOD;
	if(C9==10){
		C9="X";
	}else if(C9==11){
		C9="0";
	}
	if(entpcode9.substring(8,9)!=C9){
		return false;
	}else {
		return true;
	}
}
